Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding csproj formatting #858

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Adding csproj formatting #858

wants to merge 23 commits into from

Conversation

belav
Copy link
Owner

@belav belav commented Mar 27, 2023

closes #819

@belav belav marked this pull request as draft March 27, 2023 16:58
@belav
Copy link
Owner Author

belav commented Mar 28, 2023

The first pass at this was to just use the XmlTextWriter, which fixes indentation to be consistent, but also destroys line breaks.

https://github.com/belav/csharpier-repos/pull/63/files?diff=split&w=1

A notable example
image

Maybe instead for a first pass this can

  • Get indentation consistent
  • Clean up extra new lines - maybe new lines separating elements should be allowed, but if there are multiples, then remove them. It seems pretty common for people to leave a blank line between <ItemGroup> elements.
  • Get the end element tags consistent - <Tag/> should be <Tag />

I think that should be pretty easy to do using the existing Doc system.

Then the two remaining things I think are also needed

  • A way to specify indentation options by file type. At work we use 2 spaces in csproj, but 4 spaces in cs.
  • Maybe a way to specify which file types csharpier should format. I am leaning towards auto including cs, csproj, props and targets. The csharpierignore file is one way someone could opt out of formatting the none c# files. Maybe there should be an option instead? Something like
# this would be the default value
filesToFormat: 
  - cs
  - csproj
  - props
  - targets

Do you have any thoughts @shocklateboy92 ?

@belav
Copy link
Owner Author

belav commented Mar 28, 2023

Actually, maybe more like what prettier does. Which can be combined with options for files, and at some point also allow custom formatters

tabWidth: 4
overrides:
  - files: "*.csproj"
    options:
      tabWidth: 2
  - files: ["*.props", "*.targets"]
    formatter: none
  - files: "*.ps1"
    formatter: PluginThatFormatsPowershell

@gdlol
Copy link

gdlol commented Oct 24, 2023

😃 Hi there, I am using both csharpier and prettier in my projects and wondering if this would be some duplicate effort.

I have been using prettier for everything besides .cs files (like JSON files), and for .csproj (XML) files there is @prettier/plugin-xml.

The plugin uses linguist-languages to identify XML file types base on extensions, and it already includes file types like .csproj, .props and .targets. The list of extensions for XML can be found here. So far the experience is pretty good.

There may also be tooling concerns (VSCode settings) for including csproj formatting in csharpier. For instance, users may need to set csharpier as the default XML formatter, but is it a good idea?

Anyway, csharpier is a nice tool, and thanks for the effort to keep improving it.

@belav
Copy link
Owner Author

belav commented Nov 1, 2023

😃 Hi there, I am using both csharpier and prettier in my projects and wondering if this would be some duplicate effort.

I have been using prettier for everything besides .cs files (like JSON files), and for .csproj (XML) files there is @prettier/plugin-xml.

My initial pass for this was just intended to clean up whitespace and indentation, but I already ran into reasons why I think it needs to do a bit more than that and can probably use their code as a starting point. Thanks for sharing the link!

Even if prettier already supports this type of formatting I still see value in csharpier providing the functionality. There are situations where someone may not want to deal with node/npm and still be able to format xml. For a web app, they probably already have node but there are plenty of other types projects out there.

# Conflicts:
#	Directory.Build.props
#	Scripts/UpdateCSharpierRepos.ps1
#	Src/CSharpier.Cli/CommandLineFormatter.cs
#	Src/CSharpier.Generators/CSharpier.Generators.csproj
#	Src/CSharpier.Playground/ClientApp/src/AppContext.ts
#	Src/CSharpier.Playground/ClientApp/src/Controls.tsx
#	Src/CSharpier.Playground/ClientApp/src/FormatCode.ts
#	Src/CSharpier.Playground/Controllers/FormatController.cs
#	Src/CSharpier.Tests.Generators/CSharpier.Tests.Generators.csproj
#	Src/CSharpier.Tests.Generators/FormattingTestsGenerator.cs
#	Src/CSharpier.Tests/CodeFormatterTests.cs
#	Src/CSharpier.Tests/FormattingTests/BaseTest.cs
#	Src/CSharpier.Tests/Samples/AllInOne.test
#	Src/CSharpier/CSharpFormatter.cs
#	Src/CSharpier/CodeFormatter.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potentially support formatting .csproj files
2 participants